#!/usr/bin/env python

import sys
import threading
import signal
from optparse import OptionParser

from comm.CommDispatcher import CommDispatcher
from MainController import MainController
from ui.touchosc.touchosc_main import TouchOSC
#from ui.mrmr.mrmr_server import mrmrController
from ui.iOSCControl.iOSCController import iOSCController

def build_gtk():
    import gtk
    gtk.gdk.threads_init()
    from ui.gtk.mainwindow import MainWindow
    comm = CommDispatcher(parse_args=parse_args)
    mc = MainController(comm=comm, parse_args=parse_args)
    tosc = TouchOSC(MainController=mc)
    #mrmr = mrmrController(MainController=mc)
    iosc = iOSCController(MainController=mc)
    w = MainWindow(DeviceSystem=mc.DeviceSystem, MainController=mc, comm=comm)
    w.window.connect('destroy', mc.on_app_exit)
    gtk.main()
    print 'post gtkmain'
    
def build_qt():
    pass
    
def build_kivy():
    from ui.kivy.mainwindow import MainWindow
    w = MainWindow()

def build_daemon():
    from daemon import Daemon
    comm = CommDispatcher(parse_args=parse_args)#osc_kwargs=dict(app_address='openlightingdesigner', root_address='daemon'))
    mc = MainController(comm=comm, parse_args=parse_args)
    tosc = TouchOSC(MainController=mc)
    d = Daemon(MainController=mc)
    #comm.do_connect()
    
def build_osc_ola_host():
    from comm.dmx.OSCtoOLA import OSCtoOLAHost
    osc_ola = OSCtoOLAHost()

def on_window_destroyed(*args):
    pass

parser = OptionParser()
parser.add_option('-d', '--daemon', dest='daemon', default=False, action='store_true', help='daemon mode')
parser.add_option('-f', '--file', dest='filename', metavar='FILE', help='file to load')
o, a = parser.parse_args()
parse_args = {'options':o, 'args':a}

if __name__ == '__main__':
    if o.daemon:
        build_daemon()
    elif 'osc_ola' in sys.argv:
        build_osc_ola_host()
    elif 'kivy' in sys.argv:
        build_kivy()
    else:
        build_gtk()
    #reactor.run()
